package com.asa.mvvm_aop_frame.basic.ui

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.asa.mvvm_aop_frame.basic.BaseContract
import com.asa.mvvm_aop_frame.basic.R
import com.asa.mvvm_aop_frame.basic.databinding.BaseActivityBinding
import com.asa.mvvm_aop_frame.basic.viewmodel.BaseViewModel
import org.koin.android.viewmodel.ext.android.getViewModel
import kotlin.reflect.KClass

/**
 * create By：anderson
 * on 2022/4/24
 * desc:
 */
abstract class BaseActivity<V:ViewDataBinding,VM: BaseViewModel>:AppCompatActivity(),BaseContract.IWindow,BaseContract.IView,BaseContract.IBar {


    protected lateinit var viewModel:VM
    protected lateinit var binding:V
    val activity:AppCompatActivity = this@BaseActivity


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        windowAnimations()?.let {
            window.setWindowAnimations(it)
        }?:overridePendingTransition(0,0)
        initParams()
        initDataBindingAndViewModel()
        initView(savedInstanceState)
        initData()
        initViewObservable()
    }

    override fun onStart() {
        super.onStart()
        windowAnimations()?:overridePendingTransition(0,0)
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    }

    private fun initDataBindingAndViewModel() {
        val baseBinding = DataBindingUtil.inflate<BaseActivityBinding>(layoutInflater, R.layout.base_activity,null,false)
        val binding = DataBindingUtil.inflate<V>(layoutInflater,getLayoutId(),null,false)
        val params = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT)
        baseBinding.base.addView(binding.root,params)
        setContentView(baseBinding.root)

        // koin写法，需要 kotlin-reflect 和 koin，需要startKoin，并且配置modules
        val clazz = javaClass.kotlin.supertypes[0].arguments[1].type!!.classifier!! as KClass<VM>
        viewModel = getViewModel(clazz)
        binding.setVariable(initVariableId(),viewModel)
        binding.lifecycleOwner = this
        lifecycle.addObserver(viewModel)
    }

    fun showFragment(fragment:Fragment){
        showFragment(fragment,null,null)
    }


    fun showFragment(fragment: Fragment,enterAnimResId:Int?,exitAnimResId:Int?){
        val beginTransaction = supportFragmentManager.beginTransaction()
        if (enterAnimResId != null &&exitAnimResId != null){
            beginTransaction.setCustomAnimations(enterAnimResId,exitAnimResId)
        }
        beginTransaction.show(fragment).commitAllowingStateLoss()
    }

    fun hideFragment(fragment: Fragment){
        supportFragmentManager.beginTransaction()
            .hide(fragment).commitAllowingStateLoss()
    }

    fun loadRootFragment(containerId:Int,fragment: Fragment){
        supportFragmentManager.beginTransaction().add(containerId,fragment).hide(fragment).commitAllowingStateLoss()
    }

}